package com.woniuxy.microcurrentserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.bean.current.PageInfos;
import com.woniuxy.microcurrentserver.entity.CurrentFundAll;
import com.woniuxy.microcurrentserver.entity.CurrentFundHistory;
import com.woniuxy.microcurrentserver.entity.CurrentFundInfo;
import com.woniuxy.microcurrentserver.entity.CurrentFundMsgType;
import com.woniuxy.microcurrentserver.service.CurrentFundAllService;
import com.woniuxy.microcurrentserver.util.FailEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (CurrentFundAll)表控制层
 *
 * @author makejava
 * @since 2021-04-08 11:29:25
 */
@RefreshScope
@Slf4j
@RestController
@RequestMapping(value = "currentFundAll", produces = MediaType.APPLICATION_JSON_VALUE)
public class CurrentFundAllController {
    /**
     * 服务对象
     */
    @Resource
    private CurrentFundAllService currentFundAllService;
    @Resource
    private RestTemplate restTemplate;


    /**
     * 通过基金类型查询数据
     *
     * @param page
     * @return 多条数据
     */
    @GetMapping(value = "selectAll", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean selectAll(Integer page, String name) {
        PageInfo<CurrentFundAll> listPageInfo = this.currentFundAllService.selectAllByType(page, name);
        return ResultBean.builder().msg(FailEnum.SUCCESS.getDesc()).code(FailEnum.SUCCESS.getCode()).data(listPageInfo).build();
    }


    //    /**
//     * 新增基金数据
//     *
//     * @param
//     * @return 单条数据
//     */
//    @GetMapping(value = "insert", produces = MediaType.APPLICATION_JSON_VALUE)
//    public String insertAll(@ApiParam("基金数据") List<CurrentFundAll> list) {
//        boolean i = this.currentFundAllService.insertAll(list);
//        if (i) {
//            return JSONObject.toJSONString(ResultBean.builder().msg(FailEnum.SUCCESS.getDesc()).code(FailEnum.SUCCESS.getCode()));
//        }
//        return JSONObject.toJSONString(ResultBean.builder().msg(FailEnum.INSERT_ERROR.getDesc()).code(FailEnum.INSERT_ERROR.getCode()));
//    }
    @PostMapping("/insertNewFund")
    public ResultBean getFundInfo(@RequestBody PageInfos pageInfos) {
        System.out.println(pageInfos.getCode());
        boolean a = currentFundAllService.checkCode(pageInfos.getCode());

        if (!a) {
            String url = "https://api.doctorxiong.club/v1/fund/detail?code=" + pageInfos.getCode();
            CurrentFundMsgType res = restTemplate.getForObject(url, CurrentFundMsgType.class);
            CurrentFundInfo data = res.getData();


            List<CurrentFundHistory> millionCopiesIncome = new ArrayList<>();
            List<CurrentFundHistory> sevenDaysYearIncome = new ArrayList<>();


            for (String[] item :
                    data.getMillionCopiesIncomeData()) {
                CurrentFundHistory currentFundHistory = new CurrentFundHistory();
                currentFundHistory.setCode(pageInfos.getCode());
                currentFundHistory.setUpdate(item[0]);
                currentFundHistory.setMillionCopiesIncome(item[1]);
                millionCopiesIncome.add(currentFundHistory);
            }

            for (String[] item :
                    data.getSevenDaysYearIncomeData()) {
                CurrentFundHistory currentFundHistory = new CurrentFundHistory();
                currentFundHistory.setCode(pageInfos.getCode());
                currentFundHistory.setUpdate(item[0]);
                currentFundHistory.setSevenDaysYearIncome(item[1]);
                sevenDaysYearIncome.add(currentFundHistory);
            }

//        System.out.println(sevenDaysYearIncome.toString());
//        System.out.println(millionCopiesIncome.toString());
            currentFundAllService.insertInfo(data, millionCopiesIncome, sevenDaysYearIncome);

            return ResultBean.builder().msg("新增成功").code(200).build();
        } else {
            return ResultBean.builder().msg("该活期已存在").code(200).build();
        }
    }
}
